package com.atzly.community.controller;

import com.atzly.community.constant.CommentConstant;
import com.atzly.community.entity.Comment;
import com.atzly.community.entity.DiscussPost;
import com.atzly.community.entity.Event;
import com.atzly.community.event.EventProducter;
import com.atzly.community.service.CommentService;
import com.atzly.community.service.DiscussPostService;
import com.atzly.community.utils.HostHolder;
import com.atzly.community.utils.RedisKeyUtils;
import com.atzly.community.utils.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

/**
 * 回帖
 * 补充: 系统通知功能
 * 补充: 发布评论会修改帖子数量
 *
 * @author zlysimida
 * @date 2021/8/29 - 14:16
 */
@Controller
@RequestMapping("/comment")
public class CommentController implements CommentConstant {

    @Autowired
    private CommentService commentService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private SensitiveFilter sensitiveFilter;

    @Autowired
    private EventProducter producter;

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private RedisTemplate redisTemplate;

    // 异步添加评论
    @PostMapping("/add/{discussId}")
    public String addComment(@PathVariable("discussId") int discussId, Comment comment) {
        // 会出现空指针异常
        comment.setUserId(hostHolder.getUserFromThread().getId());
        comment.setStatus(0);
        comment.setCreateTime(new Date());
        String content = comment.getContent();
        String filterContent = sensitiveFilter.filter(content);
        comment.setContent(filterContent);
        commentService.saveComment(comment, discussId);

        // 系统通知事件
        Event event = new Event()
                .setUserId(hostHolder.getUserFromThread().getId())
                .setTopic(TOPIC_COMMENT_KEY)
                .setEntityType(comment.getEntityType())
                .setEntityId(comment.getEntityId())
                .setData("postId", discussId);

        // 保存entityUserId
        if (comment.getEntityType() == ENTITY_TYPE_POST) {
            DiscussPost target = discussPostService.selectDiscussDetails(comment.getEntityId());
            event.setEntityUserId(target.getUserId());
        } else if (comment.getEntityType() == ENTITY_TYPE_COMMENT) {
            Comment target = commentService.selectCommuntById(comment.getEntityId());
            event.setEntityUserId(target.getUserId());
        }
        producter.sendMessage(event);

        // 帖子通知事件
        if (comment.getEntityType() == ENTITY_TYPE_POST) {
            // 发帖成功触发事件 主要更新es中的当前评论的帖子信息因为增加了评论保证了es中当前评论帖子的时效性
            event = new Event()
                    .setTopic(TOPIC_PUBLISH)
                    .setUserId(comment.getUserId())
                    .setEntityType(ENTITY_TYPE_POST)
                    .setEntityId(discussId);
            producter.sendMessage(event);

            // 评论需要保存到redis中用于刷新
            String redisKey = RedisKeyUtils.getPostScoreKey();
            redisTemplate.opsForSet().add(redisKey, discussId);
        }

        return "redirect:/discuss/show/" + discussId;
    }

}
